﻿@model Cxuu.Models.EditArticleAndContent
@{
    var cateInfo = ViewBag.cateInfo as ArticleCate;
    var getByUid = ViewBag.getByUid as Article;
}
<div class="content-top-nav">
    <div class="layui-row">
        <div class="layui-col-md11">
            <h3 style="color:#444">
                <i class="layui-icon layui-icon-home"></i>
                信息报送编辑
            </h3>
        </div>
        <div class="layui-col-md1" style="text-align:right;">
            <button class="layui-btn layui-btn-sm" onclick="loadMain('/Admin/Article/Index?cid=@cateInfo.Id')">返回列表</button>
        </div>
    </div>
    <hr>
</div>

<div class="layui-fluid" style="margin-bottom:50px;">
    <div class="layui-form">
        <form class="layui-form" action="" lay-filter="FormEdit" enctype="multipart/form-data">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label asp-for="Cid" class="layui-form-label required"> </label>
                    <div class="layui-input-block">
                        <input asp-for="Cid" type="hidden">
                        <div class="layui-form-mid layui-word-aux" style="color:black !important;font-weight:bold;font-size:14px;">@cateInfo.Name</div>
                        <div class="layui-form-mid layui-word-aux" id="docNumber" style="color:red !important;font-weight:bold;font-size:20px;">
                            第 @ViewBag.DocNumber 期
                            <span style="font-size:12px;color:#333;">此为系统推测，以提交后为准</span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label asp-for="Title" class="layui-form-label required"></label>
                <div class="layui-input-block" style="width:1150px;">
                    <input type="text" asp-for="Title" lay-verify="required" lay-reqtext="拟文标题不能为空" placeholder="请输入拟文标题" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label asp-for="Department" class="layui-form-label required"></label>
                    <div class="layui-input-block" style="width:515px;">
                        <input type="text" asp-for="Department" lay-verify="required" lay-reqtext="发往部门不能为空" placeholder="请输入发往部门" value="@getByUid?.Department" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label asp-for="Leader" class="layui-form-label required"></label>
                    <div class="layui-input-block" style="width:500px;">
                        <input type="text" asp-for="Leader" placeholder="请输入发往领导" value="@getByUid?.Leader" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label asp-for="GreatTime" class="layui-form-label required"></label>
                    <div class="layui-input-inline">
                        <input type="text" asp-for="GreatTime" placeholder="请输入信息报送时间" value="" class="layui-input" id="GreatTime" autocomplete="off">
                    </div>
                </div>

                <div class="layui-inline">
                    <label asp-for="Miji" class="layui-form-label required"></label>
                    <div class="layui-input-inline">
                        <select asp-for="Miji" lay-verify="required">
                            <option value="0">无</option>
                            <option value="1">秘密</option>
                            <option value="2">机密</option>
                            <option value="3">绝密</option>
                        </select>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label asp-for="Auther" class="layui-form-label required"></label>
                    <div class="layui-input-inline">
                        <input type="text" asp-for="Auther" lay-verify="required" lay-reqtext="拟稿人不能为空" placeholder="请输入拟稿人" value="@getByUid?.Auther" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label asp-for="Proofread" class="layui-form-label required"></label>
                    <div class="layui-input-inline">
                        <input type="text" asp-for="Proofread" lay-verify="required" lay-reqtext="校对人不能为空" placeholder="请输入校对人" value="@getByUid?.Proofread" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label asp-for="EarlierExa" class="layui-form-label required"></label>
                    <div class="layui-input-inline">
                        <input type="text" asp-for="EarlierExa" lay-verify="required" lay-reqtext="初审人不能为空" placeholder="请输入初审人" value="@getByUid?.EarlierExa" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label asp-for="Examine" class="layui-form-label required"></label>
                    <div class="layui-input-inline">
                        <input type="text" asp-for="Examine" lay-verify="required" lay-reqtext="审签人不能为空" placeholder="请输入审签人" value="@getByUid?.Examine" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <label asp-for="Content" class="layui-form-label"></label>
                <div class="layui-input-block">
                    <textarea id="contentEdit" name="Content" class="layui-textarea" placeholder="请输入内容"></textarea>
                </div>
            </div>

            @if (await Authorizer.AuthView(PermissionKeys.Upload_Upload))
            {
                <div class="layui-form-item">
                    <label class="layui-form-label required">附件</label>
                    <div class="layui-input-block">
                        <div id="attval" class="cxuu_attmentupload"></div>
                        <input type="hidden" asp-for="Attments" value="" class="layui-input" id="Att">
                        <button type="button" class="layui-btn" id="attmentsUpload">
                            <i class="layui-icon ">&#xe608;</i>上传附件
                        </button>
                        <tip>支持多个附件上传，单个附件不超过 @ViewBag.fileSize KB！ 支持格式：@ViewBag.fileExts </tip>
                    </div>
                </div>
            }

            <div class="layui-form-item layui-form-text">
                <label asp-for="Remarks" class="layui-form-label"></label>
                <div class="layui-input-block">
                    <textarea name="Remarks" class="layui-textarea" placeholder="请输入备注"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="saveBtn" id="saveBtn"><i class="layui-icon">&#xe605;</i>提交</button>
                </div>
            </div>
            <input asp-for="Id" type="hidden">
            <input asp-for="Uid" type="hidden">
        </form>
    </div>
</div>
<script type="text/javascript" src="~/lib/tinymce/tinymce.min.js?v=5.10"></script>
<script>
    layui.define(function () {
        var form = layui.form, layer = layui.layer, $ = layui.jquery, upload = layui.upload, element = layui.element;
        var laydate = layui.laydate;
        form.render();

        tinymce.init({
            selector: '#contentEdit',
            language: 'zh_CN',
            relative_urls: false,
            remove_script_host: true,
            plugins: 'print preview searchreplace autolink directionality visualblocks visualchars image link media template code codesample table charmap hr pagebreak nonbreaking anchor insertdatetime advlist lists wordcount imagetools textpattern help emoticons autoresize  axupimgs',
            toolbar: 'code | undo redo restoredraft | cut copy paste | forecolor bold italic underline link | alignleft aligncenter alignright alignjustify outdent indent | fontselect fontsizeselect | table  charmap emoticons insertdatetime preview | image axupimgs',
            autosave_interval: "20s",//自动存稿时间
            font_formats: '微软雅黑=Microsoft YaHei,sans-serif;仿宋_GB2312=仿宋_GB2312,Microsoft YaHei,sans-serif;宋体=宋体;仿宋=仿宋;黑体=黑体',
            height: 650, //编辑器高度
            min_height: 400,
            draggable_modal: true,
            images_upload_base_path: '/',
            images_reuse_filename: true,
            images_upload_handler: function (blobInfo, succFun, failFun) {
                var xhr, formData;
                var file = blobInfo.blob();//转化为易于理解的file对象
                xhr = new XMLHttpRequest();
                xhr.withCredentials = false;
                xhr.open('POST', '/Admin/Upload/Upload?type=Tinymce&source=Content');
                xhr.onload = function () {
                    var json;
                    if (xhr.status != 200) {
                        failFun('HTTP Error: ' + xhr.status);
                        return;
                    }
                    json = JSON.parse(xhr.responseText);
                    if (!json || typeof json.location != 'string') {
                        failFun('Invalid JSON: ' + xhr.responseText);
                        return;
                    }
                    succFun(json.location);
                };
                formData = new FormData();
                formData.append('file', file, file.name);
                //console.log(xhr);
                xhr.send(formData);
            }
        });

        

        laydate.render({
            elem: '#GreatTime'
            , type: 'datetime'
            , value: new Date()
        });

        var editId = @ViewBag.id+1;
        //console.log(editId);
        if (editId > 1) {
            $.getJSON("/Admin/Article/AddOrEdit?id=@ViewBag.id&edit=1", function (res) {
                if (res.status == 1) {
                    var data = res.data;
                    form.val('FormEdit', {
                        Id: data.id,
                        Cid: data.cid,
                        Uid: data.uid,
                        Title: data.title,
                        GreatTime: data.greatTime,
                        Attments: data.attments,
                        DocNumber: data.docNumber,
                        Department: data.department,
                        Leader: data.leader,
                        Examine: data.examine,
                        Auther: data.auther,
                        Proofread: data.proofread,
                        EarlierExa: data.earlierExa,
                        Remarks: data.remarks,
                        Miji: data.miji,
                    });

                    $("#docNumber").html("第 " + data.docNumber + " 期");

                    setTimeout(() => {//直接设置不生效,需要设置定时器
                        tinymce.activeEditor.setContent(data.content)
                    }, 500);

                    $.each(res.attments, function (index, value) {
                        $('#attval').append('<span id="attidd' + value.id + '" class="layui-badge layui-bg-blue"> ID：' + value.id + ' 文件名：' + value.filename +
                            ' <a href="javascript:;" onclick="attColse(' + value.id + ')" ><i class="layui-icon layui-icon-close"></i></a></span>');
                    });
                }

            })
        } else {
            form.val('FormEdit', {
                Uid: 0,
                DocNumber: @ViewBag.DocNumber
                    });
        }


        var uploadInst = upload.render({
            elem: '#attmentsUpload'
            , url: '/Admin/Upload/Upload?type=layui&source=Content'
            , before: function (obj) {
                element.progress('upload-att', '0%'); //进度条复位
                layer.msg('上传中', { icon: 16, time: 0 });
            }
            , size: "@ViewBag.fileSize"
            , accept: 'file'
            , acceptMime: 'file/*'
            //, exts: 'zip|rar|7z|doc|docx|xlsx|xls|'
            , exts: "@ViewBag.fileExts".replace(/./g, '')
            , done: function (res) {
                if (res.status == 1) {
                    $('#attval').append('<span id="attidd' + res.pkey + '" class="layui-badge layui-bg-blue"> ID：' + res.pkey + ' 文件名：' + res.data.filename + ' <a href="javascript:;" onclick="attColse(' + res.pkey + ')" ><i class="layui-icon layui-icon-close"></i></a></span>');
                    var input = $("#Att").val();
                    if (input != "" && input != undefined) {
                        $("#Att").val($("#Att").val() + "," + res.pkey);
                    } else {
                        $('#Att').val(res.pkey);
                    }
                    // console.log(res);
                    //res.result[0].src + res.result[0].suffix + res.result[0].size
                    layer.msg(res.info);
                } else {
                    layer.msg("上传失败！");
                }
            }
            //进度条
            , progress: function (n, elem, e) {
                element.progress('upload-att', n + '%');
                if (n == 100) {
                    layer.msg('上传完毕', { icon: 1 });
                }
            }
        });

        var dataPost = function (url, data) {
            //console.log("json", data);
            $.post(url, data, function (res) {
                if (res.status == 1) {
                    $("#saveBtn").hide();
                    layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                        loadMain('/Admin/Article/Index?cid=' + res.cid);
                    });
                } else
                    layer.msg(res.msg, { icon: 2, anim: 6, time: 5000 });
            });
        }

        //监听提交
        //form.on('submit(saveBtn)', function (data) {  form.val('FormEdit')
        $('#saveBtn').click(function () {
            var field = form.val('FormEdit');
            field.Content = tinyMCE.activeEditor.getContent();
            if (editId == 1) {
                field.Id = 0;
                dataPost('/Admin/Article/CreatePost', field);
            }
            else {
                dataPost('/Admin/Article/EditPost', field);
            }
            return false;
        });
    });



    function attColse(id) {
        layer.confirm('真的删除这个附件吗？', { icon: 3, title: '警告' }, function (index) {
            $.post('/Admin/Attment/Delete', { id: id }, function (res) {
                if (res.status == 1) {
                    layer.msg("附件: " + res.msg);
                    $('#attidd' + id).remove();
                    var input = $("#Att").val();
                    var newarr = input.split(",");
                    newarr.splice($.inArray("" + id + "", newarr), 1);
                    $('#Att').val(newarr);
                } else {
                    layer.msg("附件: " + res.msg + "请查看是否有删除附件权限");
                }
            });
            layer.close(index);
        });
    }

</script>